/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.core.rtgen.entitygenerator.method;


import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.api.dataType.entity.ICefEntity;
import com.inspur.edp.cef.api.message.CefException;
import com.inspur.edp.cef.generator.repository.RepositoryGenUtil;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;
import java.util.Map;

public class GetChildEntityGenerator extends ClassMethodGenerator {
	private java.util.HashMap<String, String> childEntitys;

	public GetChildEntityGenerator(java.util.HashMap<String, String> childEntitys) {
		this.childEntitys = childEntitys;
	}

	@Override
	protected String getMethodName() {
		return "innerGetChildEntity";
	}

	@Override
	protected TypeInfo getReturnType() {
		return new TypeInfo(ICefEntity.class);
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
		ArrayList<Modifier.ModifierKeyword> list = new ArrayList<Modifier.ModifierKeyword>();
		list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
		return list;
	}

	@Override
	protected ArrayList<AnnotationInfo> getAttributeList() {
		return RepositoryGenUtil.buildOverrideAnnotationInfo(ast);
	}

	@Override
	protected ArrayList<ParameterInfo> getParameterCollection() {
		ParameterInfo tempVar = new ParameterInfo();
		tempVar.setParamType(new TypeInfo(String.class));
		tempVar.setParamName("code");
		ParameterInfo tempVar2 = new ParameterInfo();
		tempVar2.setParamType(new TypeInfo(String.class));
        tempVar2.setParamName("id");
		return new java.util.ArrayList<ParameterInfo>(java.util.Arrays.asList(new ParameterInfo[]{tempVar, tempVar2}));
	}

	@Override
	protected Block buildMethodBody() {
		Block block = ast.newBlock();
		block.statements().add(getSwitchStatement());
		return block;
	}

	private SwitchStatement getSwitchStatement() {
		SwitchStatement switchStatement = ast.newSwitchStatement();
		switchStatement.setExpression(ast.newSimpleName("code"));
		java.util.ArrayList<Statement> caseSections = getCaseSections();
		switchStatement.statements().addAll(caseSections);
		return switchStatement;
	}

	private java.util.ArrayList<Statement> getCaseSections() {
		java.util.ArrayList<Statement> sections = new java.util.ArrayList<Statement>();
		for (Map.Entry<String, String> childEntity : childEntitys.entrySet()) {
			addCaseSection(childEntity.getKey(), childEntity.getValue(), sections);
		}

		addDefautSection(sections);
		return sections;
	}


	//return new entityName("id");
	private void addCaseSection(String nodeCode, String entityName, ArrayList<Statement> statements) {
		SwitchCase caseStatement = ast.newSwitchCase();
		caseStatement.setExpression(RepositoryGenUtil.createStringLiteral(ast, nodeCode));
		statements.add(caseStatement);
		ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
		classInstanceCreation.setType(ast.newSimpleType(ast.newSimpleName(entityName)));
		classInstanceCreation.arguments().add(RepositoryGenUtil.createVariableLiteral(ast, "id"));
		ReturnStatement statement = ast.newReturnStatement();
		statement.setExpression(classInstanceCreation);
		statements.add(statement);
	}

	private void addDefautSection(ArrayList<Statement> statements) {
		SwitchCase defaultCase = ast.newSwitchCase();
		defaultCase.setExpression(null);
		addImport(CefException.class.getTypeName());
		Statement throwRuntimeException = RepositoryGenUtil.createThrowRuntimeException(ast, RepositoryGenUtil.createStringLiteral(ast, "找不到子节点"));
		statements.add(defaultCase);
		statements.add(throwRuntimeException);
	}
}
